'use strict';

const Service = require('egg').Service;

class UserService extends Service {
  /**
   * 根据用户名查找用户数据
   * @param {String} username
   * @returns {Object} 
   */
  async findByUsername(username) {
    const { mysql } = this.app
    const result = await mysql.get('users', {username})
    return result
  }

  /**
   * 添加用户
   * @param {string} username 用户名
   * @param {string} password 密码(需md5加密入传入)
   * @param {string} contact 联系方式
   */
  async createUser(username, password, contact) {
    const { mysql } = this.app
    const result = await mysql.insert('users', {
      username,
      password,
      contact
    })
    return result
  }

  /**
   * 查询所有用户
   */
  async showAllUsers() {
    const { mysql } = this.app
    const result = await mysql.select('users')
    return {result}
  }

  /**
   * 
   * @param {object} row 查询条件
   */
  async updateUser(row) {
    const { mysql } = this.app
    const result = await mysql.update('users', row)
    return result
  }

  /**
   * 
   * @param {number} id 用户id
   */
  async deleteUser(id) {
    const { mysql } = this.app
    const result = await mysql.delete('users', {
      id
    })
    return result
  }

  /**
   * 
   * @param {number} id 用户id
   */
  async findById(id) {
    const { mysql } = this.app
    const result = await mysql.get('users', {
      id
    })
    return result
  }

}

module.exports = UserService;
